<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
// API 模板文件，可以复制
namespace api\home\controller;
use think\Db;
use think\Validate;
use cmf\controller\RestBaseController;

class CompanyinfoController extends RestBaseController
{

    /**
     * 根据项目ID查询企业注册登记表信息
     */
    public function register($private = null)
    {
        $validate = new Validate([
            'project_id'     => 'require',

        ]);

        $validate->message([
            'project_id.require'     => '请输入项目id!',

        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        //查询
        $register_data = Db::name("project_register")
            ->where('project_id', $data['project_id'])
            ->find();

        if (empty($register_data)) {
            $this->success('获取成功!', []);
        }



        $register_data['product'] = $register_data['product'] ? json_decode($register_data['product']) : $register_data['product'];
        $register_data['register_type'] = $register_data['register_type'] ? json_decode($register_data['register_type']) : $register_data['register_type'];



        $register_data['marketing']  = empty($register_data['marketing']) ? '': intval($register_data['marketing']);
        $register_data['venture_investment']  = empty($register_data['venture_investment']) ? '':  intval($register_data['venture_investment']);
        $register_data['high_tech']  = empty($register_data['high_tech']) ? '': intval($register_data['high_tech']);

        $register_data['foreign_id']  =  empty($register_data['foreign_id']) ? '': intval($register_data['foreign_id']);
        $register_data['industry_id']  =  empty($register_data['industry_id']) ? '': intval($register_data['industry_id']);
        $register_data['scale_id']  =  empty($register_data['scale_id']) ? '':intval($register_data['scale_id']);
        $register_data['revenue_id']  = empty($register_data['revenue_id']) ? '':intval($register_data['revenue_id']);
        $register_data['colllection_id']  = empty($register_data['colllection_id']) ? '':intval($register_data['colllection_id']);




        $register_stock_civics_data = Db::name("register_stock_civics")
            ->where('register_id', $register_data['id'])
            ->select()
            ->toArray();
        $register_data['register_stock_civics'] = $register_stock_civics_data;

        $register_stock_legal_data = Db::name("register_stock_legal")
            ->where('register_id', $register_data['id'])
            ->select()
            ->toArray();
        $register_data['register_stock_legal'] = $register_stock_legal_data;

        if ($private) {
            return $register_data;
        }
        $this->success('获取成功!', $register_data);
    }


    /**
     * 根据项目ID查询企业主情况表信息
     */
    public function condition($private = null)
    {
        $validate = new Validate([
            'project_id'     => 'require',

        ]);

        $validate->message([
            'project_id.require'     => '请输入项目id!',

        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        //查询

        $condition_data = Db::name("project_condition")
            ->where('project_id', $data['project_id'])
            ->find();
            

        if (empty($condition_data)) {
            $this->success('获取成功!',  []);
        }



        if ($private) {
            return $condition_data;
        }
        $this->success('获取成功!', $condition_data);
    }



    /**
     * 根据项目ID查询知识产权汇总表
     */
    public function knowledge($private = null)
    {
        $validate = new Validate([
            'project_id'     => 'require',

        ]);

        $validate->message([
            'project_id.require'     => '请输入项目id!',

        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        //查询
        $knowledge_data = Db::name("project_knowledge")
            ->where('project_id', $data['project_id'])
            ->find();

        if (empty($knowledge_data)) {
            $this->success('获取成功!', []);
        }

        $knowledge_data['one_invent_patent'] = $knowledge_data['one_invent_patent'] ? json_decode($knowledge_data['one_invent_patent']) : $knowledge_data['one_invent_patent'];
        $knowledge_data['one_plant_variety'] = $knowledge_data['one_plant_variety'] ? json_decode($knowledge_data['one_plant_variety']) : $knowledge_data['one_plant_variety'];
        $knowledge_data['one_defence_variety'] = $knowledge_data['one_defence_variety'] ? json_decode($knowledge_data['one_defence_variety']) : $knowledge_data['one_defence_variety'];
        $knowledge_data['one_medicine_variety'] = $knowledge_data['one_medicine_variety'] ? json_decode($knowledge_data['one_medicine_variety']) : $knowledge_data['one_medicine_variety'];
        $knowledge_data['one_new_medicine'] = $knowledge_data['one_new_medicine'] ? json_decode($knowledge_data['one_new_medicine']) : $knowledge_data['one_new_medicine'];
        $knowledge_data['one_circuit_design'] = $knowledge_data['one_circuit_design'] ? json_decode($knowledge_data['one_circuit_design']) : $knowledge_data['one_circuit_design'];
        $knowledge_data['two_software_copyright'] = $knowledge_data['two_software_copyright'] ? json_decode($knowledge_data['two_software_copyright']) : $knowledge_data['two_software_copyright'];
        $knowledge_data['one_crop_variety'] = $knowledge_data['one_crop_variety'] ? json_decode($knowledge_data['one_crop_variety']): $knowledge_data['one_crop_variety'];
        $knowledge_data['two_facade_variety'] = $knowledge_data['two_facade_variety'] ? json_decode($knowledge_data['two_facade_variety']): $knowledge_data['two_facade_variety'];



        $knowledge_info_data = Db::name("knowledge_info")
            ->where('knowledge_id', $knowledge_data['id'])
            ->select()
            ->toArray();
        if ($knowledge_info_data) {
            foreach ($knowledge_info_data as &$val) {
                $val['info_accessory']    = $val['info_accessory'] ? json_decode($val['info_accessory']) : '';
            }
            unset($val);
        }


        $knowledge_data['knowledge_info'] = $knowledge_info_data;

        $knowledge_condition_data = Db::name("knowledge_condition")
            ->where('knowledge_id', $knowledge_data['id'])
            ->select()
            ->toArray();

        if($knowledge_condition_data) {
            foreach ($knowledge_condition_data as &$val) {
                $val['condition_accessory']    = $val['condition_accessory'] ? json_decode($val['condition_accessory']) : '';
            }
            unset($val);
        }


        $knowledge_data['knowledge_condition'] = $knowledge_condition_data;

        if ($private) {
            return $knowledge_data;
        }

        $this->success('获取成功!', $knowledge_data);
    }





    /**
     * 根据项目ID查询备注表信息
     */
    public function remark()
    {
        $validate = new Validate([
            'project_id'     => 'require',
            'remark_name'     => 'require',
        ]);

        $validate->message([
            'project_id.require'     => '请输入项目id!',
            'remark_name.require'     => '请输入要查的备注表名!',
        ]);

        $data = $this->request->param();

        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }


        switch ($data['remark_name']) {
            case 1:
                // code...
                $data['remark_name'] = 'project_register_remark';
                break;
            case 2:
                // code...
                $data['remark_name'] = 'project_condition_remark';
                break;
            case 3:
                $data['remark_name'] = 'project_knowledge_remark';
                break;

            default:
                // code...
                $this->error('请输入要查的备注表名!');
                break;
        }

        //查询
        $project_remark_data = Db::name($data['remark_name'])
            ->where('project_id', $data['project_id'])
            ->find();

        $this->success('获取成功!', $project_remark_data);

    }








}
